<?php
/*
 * Created on Feb 4, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 	$title = 'Thông tin chung';
	include_once ( "../js/fckeditor/fckeditor.php");

 	include('../includes/session.php');
	if($session->logged_in){
		if($session->userlevel == 1){
			header("Location: ../index.php");
		}
	}else{
		header("Location: login.php");
	}	
	//include('../includes/encrypturl.php');
	include('../models/info_model.php');
	$session->setHeader(4);	
 	include('../includes/header.inc');
	
	if (isset($_GET['SelectedIfd'])){
		$SelectedIfd =strtoupper($_GET['SelectedIfd']);
	} elseif(isset($_POST['SelectedIfd'])){
		$SelectedIfd =strtoupper($_POST['SelectedIfd']);
	}
	if (isset($_GET['SelectedIfdc'])){
		$SelectedIfdc =strtoupper($_GET['SelectedIfdc']);
	} elseif(isset($_POST['SelectedIfdc'])){
		$SelectedIfdc =strtoupper($_POST['SelectedIfdc']);
	}

	echo '<div id="wrapper">';
	echo '<div id="content">';
	echo '<div id="box">';
	
	if (isset($_GET['delete']) && $SelectedIfdc) {
		if($infoModel->deleteInfo($SelectedIfdc) == 0){
			echo prnMsg(_('Thông tin < '.$SelectedIfdc.' > đã xóa.'),'success');
		}else{
			echo prnMsg(_('Thông tin < '.$SelectedIfdc.' > chưa xóa.'),'success');
		}				
		unset ($SelectedIfdc);
		unset($_GET['delete']);
	}
	
	if (!isset($SelectedIfd)) {
		echo '<center><h3>Vui lòng chọn loại thông tin</h3></center>';
	}
	
	if (isset($SelectedIfd) && !isset($SelectedIfdc)) {
		
		$start=$_GET['start'];
		if(strlen($start) > 0 and !is_numeric($start)){
			echo "Data Error";
			exit;
		}		
		
		$eu = ($start - 0); 
		$limit = $number_record_perpage;                                 
		$this1 = $eu + $limit; 
		$back = $eu - $limit; 
		$next = $eu + $limit;
				
		$num = $infoModel->getCountInfo($SelectedIfd);		
		$result = $infoModel->getInfos($SelectedIfd, $eu, $limit);
		
		echo '<h3>Danh sách thông tin chung</h3>';
		echo '<table width="100%"><thead>';
			echo '<tr class="borderBottom">';
	        echo '<th><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
	        echo '<th><a href="#">Tiêu đề</a></th>';
	        echo '<th><a href="#">Hiển thị tóm tắt</a></th>';	 
			echo '<th><a href="#">Thứ tự</a></th>';     
			echo '<th><a href="#">Đặc biệt</a></th>';     
	        echo '<th><a href="#">Ngày đăng ký</a></th>';
			echo '<th><a href="#">Ngày cập nhật</a></th>';	        
	        echo '<th>><a href="#">Hành động</a></th>';
	        echo '</tr></thead>';
			
			while ($myrow = $infoModel->getArrayInfos($result)) {				
				printf('<tbody><tr class="borderBottom">
						<td class="a-center">%s</td>						
	                    <td>%s</td>
	                    <td>%s</td>
						<td>%s</td>	                       
	                    <td>%s</td>
						<td>%s</td>
						<td>%s</td>
	                    <td><a href="%sSelectedIfd=%s&SelectedIfdc=%s"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Edit user" width="16" height="16" /></a>
							<a href="%sSelectedIfd=%s&SelectedIfdc=%s&delete=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Delete user" width="16" height="16" /></a></td>
	                    </tr></tbody>',
						$myrow[0],
						$myrow[2],
						$myrow[4],
						$myrow[6],
						$myrow[7],
						ConvertSQLDate($myrow[9]),
						ConvertSQLDate($myrow[10]),					
						$_SERVER['PHP_SELF'] . '?' . SID . '&',
						$SelectedIfd,
						$myrow[0],
						$_SERVER['PHP_SELF'] . '?' . SID . '&',
						$SelectedIfd,
						$myrow[0]);				
			}
			
		echo '</table>';
		if($num > $limit ){
			echo '<div id="pager">Trang ';
			if($back >=0) {  
				echo '<a href="'.$_SERVER['PHP_SELF'].'?&SelectedIfd='.$SelectedIfd.'&start='.$back.'"><img src="'.$PathPrefix.'img/icons/arrow_left.gif" width="16" height="16" /></a>';
			};
			$i=0;
			$l=1;
			for($i=0;$i < $num;$i=$i+$limit){
				if($i <> $eu){
					echo " <a href='".$_SERVER['PHP_SELF']."?&SelectedIfd=".$SelectedIfd."&start=$i'><font face='Verdana' size='2'>$l</font></a> ";
				}
				else { 
					echo "<font face='Verdana' size='4' color=red>$l</font>";				
				}
					$l=$l+1;
			}
			if($this1 < $num) {
				echo '<a href="'.$_SERVER['PHP_SELF'].'?&SelectedIfd='.$SelectedIfd.'&start='.$next.'"><img src="'.$PathPrefix.'img/icons/arrow_right.gif" width="16" height="16" /></a>';
			};
            
			 echo ' Tổng cộng <strong>'.$num.'</strong> thông tin
                    </div>';
					
		}else{
			echo '<div id="pager">Tổng cộng <strong>'.$num.'</strong> thông tin</div>';
		}    
	}
	if (isset($SelectedIfd) && isset($SelectedIfdc)) {
		echo "<div><a href='" . $_SERVER['PHP_SELF'] . '?' . SID . "&SelectedIfd=".$SelectedIfd."'><font face='Verdana' size='2' color=red>" . _('Quay về danh sách thông tin') . "</font></a></div>";
	}
	echo "</div>";
	echo '<br />';
	echo '<div id="box">';
	
	if(isset($_SESSION['success'])){
	   /* Registration was successful */
	   if($_SESSION['success']){
	   		echo prnMsg(_('Thông tin < '.$_SESSION['successname'].' > đã tạo thành công.'),'success');
	   }
	   /* Registration failed */
	   else{
	      echo prnMsg(_('Thông tin < '.$_SESSION['successname'].' > không thể tạo'),'success');
	   }
	   unset($_SESSION['success']);
	   unset($_SESSION['successname']);
	}
	
	if(isset($_SESSION['edit'])){
   		unset($_SESSION['edit']);
   		echo prnMsg(_('Thông tin < '.$_SESSION['editname'].' > cập nhật thành công'),'success');
	}
	
	if($form->num_errors > 0){
	   //echo $form->num_errors." error(s) found";
	   echo $form->error("SqlErrorMessage");
	   echo $form->error("title");
	   echo $form->error("header");
	   echo $form->error("image");
	}
	
	if (isset($SelectedIfd)) {
			echo "<form id='form' method='post' enctype='multipart/form-data' action='../controllers/info_controller.php" . "?" . SID . "'>";
			if(isset($SelectedIfdc)){

				$myrow = $infoModel->getInfoById($SelectedIfd, $SelectedIfdc);
				
				$_POST['name'] = $myrow['name'];
				$_POST['title'] = $myrow['title'];
				$_POST['image'] = $myrow['image'];
				$_POST['header'] = $myrow['header'];
				$_POST['content'] = $myrow['content'];
				$_POST['sortNo'] = $myrow['sortNo'];
				$_POST['isTop'] = $myrow['isTop'];
				
				echo "<input type=hidden name='SelectedIfdc' VALUE='" . $SelectedIfdc . "'>";				
				echo '<h3 id="adduser">Cập nhật '.$_POST['name'].'</h3><br/>';					
			}else{
				$myrow = $infoModel->getInfoTypeById($SelectedIfd);
				
				$_POST['name'] = $myrow['name'];
				
				echo '<h3 id="adduser">Thêm mục '.$_POST['name'].'</h3><br/>';	
			}
			
			echo "<input type=hidden name='SelectedIfd' VALUE='" . $SelectedIfd . "'>";
		
			if (!isset($_POST['title'])){
		  		$_POST['title']='';
			}
			if (!isset($_POST['image'])){
		  		$_POST['image']='';
			}
			if (!isset($_POST['header'])){
		  		$_POST['header']='';
			}
			if (!isset($_POST['content'])){
		  		$_POST['content']='';
			}
			if (!isset($_POST['sortNo'])){
				$_POST['sortNo']= 0;
			}
			if (!isset($_POST['isTop'])){
				$_POST['isTop']= 0;
			}
			if (!isset($_POST['createDate'])){
				$_POST['createDate']= $today;
			}
	
			echo '<fieldset id="personal">';
			
			echo '<legend>THÔNG TIN CHUNG</legend>';
			echo '<label for="title">Tiêu đề<span class="require">(*)</span>: </label><input name="title" id="title" type="text" tabindex="1" size="70" value="'.$_POST['title'].'"/><br />';			
			echo '<label for="header">Hiển thị tóm tắt<span class="require">(*)</span>: </label><input name="header" size="90" type="text" tabindex="3" value="'.$_POST['header'].'"/><br />';
			echo '<label for="sortNo">Thứ tự hiển thị<span class="require">(*)</span>: </label><input name="sortNo" size="20" type="text" tabindex="3" value="'.$_POST['sortNo'].'"/><br />';
			echo '<label for="isTop">Đặc biệt<span class="require">(*)</span>: </label><input name="isTop" size="20" type="text" tabindex="3" value="'.$_POST['isTop'].'"/><br />';
			echo '<label for="image">Chọn ảnh: </label><input type="file" name="image"/><br />';
			if (!isset($SelectedIfdc)) {				
				echo '<label for="createDate">Ngày tạo: </label><input name="createDate" id="createDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$_POST['createDate'].'"/>';
				echo '<a href="javascript:NewCal(\'createDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';
				echo '<input type="hidden" name="infoadd" value="1" />';
			}else{				
				if (($_POST['image'] != null) && file_exists($_SESSION['PathUploadContent'].$_POST['image'])) {	
					echo '<label for="image">Ảnh: </label><img src="'.$_SESSION['PathUploadContent'].$_POST['image'].'" alt="Image Content" width="'.IMAGE_WIDTH.'" height="'.IMAGE_HEIGHT.'"  /><br />';
				}else{
					echo '<label for="image">Ảnh: </label><img src="../img/NoPhotoAvailable.jpg" alt="Image Content" width="'.IMAGE_WIDTH.'" height="'.IMAGE_HEIGHT.'"  /><br />';
				}				
				echo '<input type="hidden" name="infoedit" value="1" />';
			}
			
			$oFCKeditor = new FCKeditor('content');
			$oFCKeditor->BasePath = $PathPrefix.'js/fckeditor/';
			$oFCKeditor->Value = $_POST['content'];
			$oFCKeditor->Width = 700;
			$oFCKeditor->Height = 800;
			$oFCKeditor->Create();

			echo '</fieldset>';
			echo '<input name="submit" type="submit" value="Đồng ý" />';
			echo "</form";
	}
	
	echo '</div>';
	echo '</div>';
	
	include('../includes/footer.inc');
?>
